/**
 * GasAction.java
 *2013年10月6日下午12:15:48
 *LINKT
 */
package com.jskj.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.jskj.bean.Gas;
import com.jskj.bean.Xzqh;
import com.jskj.commons.BaseAction;
import com.jskj.dao.DaoGas;
import com.jskj.dao.DaoXzqh;

/**
 * @author LINKT
 *
 */
public class GasAction extends BaseAction {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String page;
        private String start;
        private String limit;
        private String jyz_sh;
        private String jyz_xzqh;
        private String jyz_bh;
        private String jyz_mc;
        private String jyz_swjg;
        private String jyz_dz;
        private String jyz_lxr;
        private String jyz_dh;
        private String jyz_bz ;
        private String jyz_bh_zx;
        private String jyz_bh_pt;
        private String node;
        /**
         * 获取加油站list
         */
        public void getGasList() {
                HttpServletRequest request=ServletActionContext.getRequest();
                HttpSession session=request.getSession();
                String sql="";
                String sql1="where 1=1 ";
                if (jyz_swjg.equals("")) {//为空则赋值session中的税务机关
                        jyz_swjg=(String)session.getAttribute("swjg_dm");
                }
                if (!jyz_swjg.equals("11305000000")) {//不是邢台地区的行政区划
                        sql1+=" and a.jyz_swjg='"+jyz_swjg+"'";
                }
                
                if (!jyz_sh.equals("")) {
                        sql1+=" and a.jyz_sh='"+jyz_sh+"'";
                }
                if (sql1.length()>0) {
                        sql="select a.*,b.swjg_mc as jyz_swjg_mc,c.xzqh_mc as jyz_xzqh_mc from jyz a "+
                                        "join dm_swjg b on (a.jyz_swjg=b.swjg_dm) "+
                                        "join xzqh c on(a.jyz_xzqh=c.xzqh_dm) "
                                        + sql1;
                }else{
                        sql="select a.*,b.swjg_mc as jyz_swjg_mc,c.xzqh_mc as jyz_xzqh_mc from jyz a "+
                                        "join dm_swjg b on (a.jyz_swjg=b.swjg_dm) "+
                                        "join xzqh c on(a.jyz_xzqh=c.xzqh_dm) ";
                }
                DaoGas daoGas=new DaoGas();
                String str=daoGas.getGasList(Integer.parseInt(page), Integer.parseInt(limit), sql);
                this.out(str);
        }
        public void getZXGasList() {
                HttpServletRequest request=ServletActionContext.getRequest();
                HttpSession session=request.getSession();
                String sql="";
                String sql1="";
                if (jyz_xzqh.equals("")) {//为空则赋值session中的行政区划
                        jyz_xzqh=(String)session.getAttribute("xzqh_dm");
                }
                if (!jyz_xzqh.equals("130500")) {//不是邢台地区的行政区划
                        sql1+=" and a.jyz_xzqh='"+jyz_xzqh+"'";
                }
                
                if (!jyz_sh.equals("")) {
                        sql1+=" and a.jyz_sh='"+jyz_sh+"'";
                }
                if (sql1.length()>0) {
                        sql="select a.*,b.swjg_mc as jyz_swjg_mc,c.xzqh_mc as jyz_xzqh_mc from jyz a "+
                                        "join dm_swjg b on (a.jyz_swjg=b.swjg_dm) "+
                                        "join xzqh c on(a.jyz_xzqh=c.xzqh_dm) "+
                                        "where a.jyz_bz='Y' "+sql1;
                }else{
                        sql="select a.*,b.swjg_mc as jyz_swjg_mc,c.xzqh_mc as jyz_xzqh_mc from jyz a "+
                                        "join dm_swjg b on (a.jyz_swjg=b.swjg_dm) "+
                                        "join xzqh c on(a.jyz_xzqh=c.xzqh_dm) "+
                                        "where a.jyz_bz='Y' ";
                }
                DaoGas daoGas=new DaoGas();
                String str=daoGas.getGasList(Integer.parseInt(page), Integer.parseInt(limit), sql);
                this.out(str);
        }
        public void getPTGasList() {
                String sql="select a.*,b.swjg_mc as jyz_swjg_mc,c.xzqh_mc as jyz_xzqh_mc from jyz a "+
                                        "join dm_swjg b on (a.jyz_swjg=b.swjg_dm) "+
                                        "join xzqh c on(a.jyz_xzqh=c.xzqh_dm) "+
                                        "where a.jyz_bh in(select jyz_ptz from jyzgx where jyz_zxz='"+jyz_bh_zx+"')";
                DaoGas daoGas=new DaoGas();
                String str=daoGas.getGasList(Integer.parseInt(page), Integer.parseInt(limit), sql);
                this.out(str);
        }
        public void getAllPTGasList(){
                String str="";
                HttpServletRequest request=ServletActionContext.getRequest();
                HttpSession session=request.getSession();
                Xzqh xzqh=new Xzqh();
                DaoXzqh daoXzqh=new DaoXzqh();
                DaoGas daoGas=new DaoGas();
                xzqh=daoXzqh.getXzqhforDm((String)session.getAttribute("xzqh_dm"));
                if (node.equals("root")){
                        str="[{\"id\":\""+xzqh.getXzqh_dm()+"\",\"text\":\""+xzqh.getXzqh_mc()+"\"}]";
                }else{
                        if (node.equals("130500")) {
                                str=daoXzqh.getXzqhListNoLeaf(node);
                        }else{
                                str=daoGas.getGasListForLeaf(node);
                        }
                }
                this.out(str);
        }
        public void getAllZXGasList(){
                String str="";
                HttpServletRequest request=ServletActionContext.getRequest();
                HttpSession session=request.getSession();
                Xzqh xzqh=new Xzqh();
                DaoXzqh daoXzqh=new DaoXzqh();
                DaoGas daoGas=new DaoGas();
                xzqh=daoXzqh.getXzqhforDm((String)session.getAttribute("xzqh_dm"));
                if (node.equals("root")){
                        //str="[{\"id\":\""+xzqh.getXzqh_dm()+"\",\"text\":\""+xzqh.getXzqh_mc()+"\"}]";
                        if (xzqh.getXzqh_dm().equals("130500")) {
                                str=daoXzqh.getXzqhListNoLeaf("130500");
                        }else{
                                str=daoGas.getZXGasListForLeaf(xzqh.getXzqh_dm());
                        }
                }else{
                        if (node.equals("130500")) {
                                str=daoXzqh.getXzqhListNoLeaf(node);
                        }else{
                                str=daoGas.getZXGasListForLeaf(node);
                        }
                }
                this.out(str);
        }
        /**
         * 新增加油站
         */
        public void addGas() {
                Gas gas=new Gas();
                gas.setJyz_bh(jyz_bh);
                gas.setJyz_bz(jyz_bz);
                gas.setJyz_dh(jyz_dh);
                gas.setJyz_dz(jyz_dz);
                gas.setJyz_lxr(jyz_lxr);
                gas.setJyz_mc(jyz_mc);
                gas.setJyz_sh(jyz_sh);
                gas.setJyz_swjg(jyz_swjg);
                gas.setJyz_xzqh(jyz_xzqh);
                DaoGas daoGas=new DaoGas();
                if (daoGas.addGas(gas)) {
                        this.out("{\"success\":true,\"msg\":\"加油站信息添加成功。\"}");
                }else {
                        this.out("{\"success\":false,\"msg\":\"加油站信息添加失败,加油站编号重复。\"}");
                }
                
        }

        public void modifGas() {
                Gas gas=new Gas();
                gas.setJyz_bh(jyz_bh);
                gas.setJyz_bz(jyz_bz);
                gas.setJyz_dh(jyz_dh);
                gas.setJyz_dz(jyz_dz);
                gas.setJyz_lxr(jyz_lxr);
                gas.setJyz_mc(jyz_mc);
                gas.setJyz_sh(jyz_sh);
                gas.setJyz_swjg(jyz_swjg);
                gas.setJyz_xzqh(jyz_xzqh);
                DaoGas daoGas=new DaoGas();
                if (daoGas.updateGas(gas)) {
                        this.out("{\"success\":true,\"msg\":\"加油站信息修改成功。\"}");
                }else {
                        this.out("{\"success\":false,\"msg\":\"加油站信息添修改失败,加油站编号未找到。\"}");
                }
        }
        public void delGas() {
                Gas gas=new Gas();
                gas.setJyz_bh(jyz_bh);
                gas.setJyz_bz(jyz_bz);
                gas.setJyz_dh(jyz_dh);
                gas.setJyz_dz(jyz_dz);
                gas.setJyz_lxr(jyz_lxr);
                gas.setJyz_mc(jyz_mc);
                gas.setJyz_sh(jyz_sh);
                gas.setJyz_swjg(jyz_swjg);
                gas.setJyz_xzqh(jyz_xzqh);
                DaoGas daoGas=new DaoGas();
                if (daoGas.delGas(gas)) {
                        this.out("{\"success\":true,\"msg\":\"加油站信息删除成功。\"}");
                }else {
                        this.out("{\"success\":false,\"msg\":\"加油站信息删除失败,加油站编号未找到。\"}");
                }
        }

        public void addPTGas() {
                if (jyz_bh_pt.length()==6) {
                        this.out("{\"success\":false,\"msg\":\"普通加油站新增关联失败,请选择正确的普通加油站。\"}");
                        return;
                }
                String [] jyz_pt=jyz_bh_pt.split(",");
                
                DaoGas daoGas=new DaoGas();
                for (int i = 0; i < jyz_pt.length; i++) {
                        daoGas.addPTGas(jyz_bh_zx, jyz_pt[i]);
                }
                this.out("{\"success\":true,\"msg\":\"普通加油站新增关联成功。\"}");
/*                if (daoGas.addPTGas(jyz_bh_zx, jyz_bh_pt)) {
                        this.out("{\"success\":true,\"msg\":\"普通加油站新增关联成功。\"}");
                }else{
                        this.out("{\"success\":false,\"msg\":\"普通加油站新增关联失败,该加油站已经关联。\"}");
                }*/
        }
        public void delPTGas(){
                DaoGas daoGas=new DaoGas();
                if (daoGas.delPTGas(jyz_bh_zx, jyz_bh_pt)) {
                        this.out("{\"success\":true,\"msg\":\"普通加油站删除关联成功。\"}");
                }else{
                        this.out("{\"success\":false,\"msg\":\"普通加油站删除关联失败,该加油站未关联。\"}");
                }
        }
        public String getPage() {
                return page;
        }


        public void setPage(String page) {
                this.page = page;
        }


        public String getStart() {
                return start;
        }


        public void setStart(String start) {
                this.start = start;
        }


        public String getLimit() {
                return limit;
        }


        public void setLimit(String limit) {
                this.limit = limit;
        }


        public String getJyz_sh() {
                return jyz_sh;
        }


        public void setJyz_sh(String jyz_sh) {
                this.jyz_sh = jyz_sh;
        }


        public String getJyz_xzqh() {
                return jyz_xzqh;
        }


        public void setJyz_xzqh(String jyz_xzqh) {
                this.jyz_xzqh = jyz_xzqh;
        }
        public String getJyz_bh() {
                return jyz_bh;
        }
        public void setJyz_bh(String jyz_bh) {
                this.jyz_bh = jyz_bh;
        }
        public String getJyz_mc() {
                return jyz_mc;
        }
        public void setJyz_mc(String jyz_mc) {
                this.jyz_mc = jyz_mc;
        }
        public String getJyz_swjg() {
                return jyz_swjg;
        }
        public void setJyz_swjg(String jyz_swjg) {
                this.jyz_swjg = jyz_swjg;
        }
        public String getJyz_dz() {
                return jyz_dz;
        }
        public void setJyz_dz(String jyz_dz) {
                this.jyz_dz = jyz_dz;
        }
        public String getJyz_lxr() {
                return jyz_lxr;
        }
        public void setJyz_lxr(String jyz_lxr) {
                this.jyz_lxr = jyz_lxr;
        }
        public String getJyz_dh() {
                return jyz_dh;
        }
        public void setJyz_dh(String jyz_dh) {
                this.jyz_dh = jyz_dh;
        }
        public String getJyz_bz() {
                return jyz_bz;
        }
        public void setJyz_bz(String jyz_bz) {
                this.jyz_bz = jyz_bz;
        }
        public String getJyz_bh_zx() {
                return jyz_bh_zx;
        }
        public void setJyz_bh_zx(String jyz_bh_zx) {
                this.jyz_bh_zx = jyz_bh_zx;
        }
        public String getJyz_bh_pt() {
                return jyz_bh_pt;
        }
        public void setJyz_bh_pt(String jyz_bh_pt) {
                this.jyz_bh_pt = jyz_bh_pt;
        }
        public String getNode() {
                return node;
        }
        public void setNode(String node) {
                this.node = node;
        }

}
